package com.cly.handler;

import com.cly.base.contants.UserConstants;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import java.io.IOException;

/**
 * 文件请求处理器
 *
 * @author by 春风能解释
 * <p>
 * 2024/9/18
 */
@Component
public class FileHttpRequestHandler extends ResourceHttpRequestHandler {


    /**
     * 获取资源
     *
     * @param request 请求
     * @return 资源
     * @throws IOException 若发生异常
     */
    @Override
    protected Resource getResource(HttpServletRequest request) throws IOException {
        String path = (String) request.getAttribute(UserConstants.ATTR_FILE);
        return new FileSystemResource(path);
    }
}
